#include "EnvObject.h"

/*******************************************************************
					PUBLIC Class Functions
*******************************************************************/


// Release resources
void EnvObject::Release() {
	mesh.Release();
	meshRuin.Release();
	meshInst.Release();
}

// Initialize variables to their default values
bool EnvObject::Initialize(LPDIRECT3DDEVICE9 pDevice) {
	maxHealth = 10.0f;
	health = maxHealth;
	key = 0;
	value = 0;
	distToCam = 0.0f;
	diedOnce = false;
	alignment = "Neutral";
	dataFile = " ";
	if(modelFile.empty())
		modelFile = " ";
	name = "Unnamed";
	m_state = wandering;

	// Initialize mesh
	if(!InitializeMesh(pDevice))
		return false;

	return true;
}

// Initialize the mesh
bool EnvObject::InitializeMesh(LPDIRECT3DDEVICE9 pDevice) {
	if(modelFile != " ") {
		//PRINT("Reading from model file: ");
		//PRINT(modelFile.c_str());	NEXT_LINE;
		if(!mesh.Load(pDevice, modelFile)) {
			SHOWERROR("mesh.Load() - Failed", __FILE__, __LINE__);
			return false;
		}
		meshInst.setMesh(&mesh);
		float radius = 0.0f;
		radius = (meshInst.getXScale() + meshInst.getYScale() + meshInst.getZScale()) / g_BoundingRadiusModifier;
		radius *= meshInst.getBoundingRadius();
		meshInst.setBoundingRadius(radius);
	} else {
		PRINT("Model file name not specified, current entry: ");
		PRINT(modelFile.c_str());	NEXT_LINE;
		return false;
	}

	return true;
}

// Initialize the mesh
bool EnvObject::InitializeRuinMesh(LPDIRECT3DDEVICE9 pDevice) {
	PRINT("1");		NEXT_LINE;
	if(destructable) {
		PRINT("2");		NEXT_LINE;
		if(ruinFile != " ") {
			PRINT("3");		NEXT_LINE;
			if(!meshRuin.Load(pDevice, ruinFile)) {
				SHOWERROR("meshRuin.Load() - Failed", __FILE__, __LINE__);
				return false;
			}
			PRINT("4");		NEXT_LINE;
		} else {
			PRINT("Model file name not specified, current entry: ");
			PRINT(ruinFile.c_str());	NEXT_LINE;
			return false;
		}
	}
	PRINT("5");		NEXT_LINE;

	return true;
}

// General Update function, mainly for the A.I.
void EnvObject::Update(float elapsedTime) {
	if(destructable && health <= 0.0f) {
		meshInst.setMesh(&meshRuin);
	}
}

// Render the object
void EnvObject::Render(LPDIRECT3DDEVICE9 pDevice) {
	meshInst.Render(pDevice);
}
